package BeheerFysiotherapeuten.View;

import BeheerFysiotherapeuten.Model.PraktijkLijst;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.Observable;
import java.util.Observer;

public class TherapeutOverzicht extends javax.swing.JPanel implements Observer {
	private Object[][] inhoud;
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JButton jButton3;
	private javax.swing.JButton jButton4;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	
	

	public TherapeutOverzicht() {

	

		initComponents();

	}

	public void setListener(ActionListener listener) {

		jButton1.addActionListener(listener);
		jButton2.addActionListener(listener);
		jButton3.addActionListener(listener);
		jButton4.addActionListener(listener);


	}

	public Object[][] getInhoud() {
		return inhoud;

	}

	public void setInhoud(Object[][] inhoud) {
		this.inhoud = inhoud;

		jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
				new String[] {"BSN", "Voornaam", "Tussen", "Achternaam",
						"Geboortedatum", "Woonplaats" , "adres","postcode","TelefoonNr","Startdatum","Uren" }));

	}

	private void initComponents() {
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();
		jButton3 = new javax.swing.JButton();
		jButton4 = new javax.swing.JButton();
		
		jButton1.setActionCommand("Nieuw");

		jButton2.setActionCommand("Wijzig");

		jButton3.setActionCommand("Verwijder");

		jButton4.setActionCommand("Rooster");
		
		jScrollPane1 = new javax.swing.JScrollPane();

		jTable1 = new javax.swing.JTable(){
			public boolean isCellEditable(int row, int coll){
				return false;
			}
		};
		jTable1.getTableHeader().setReorderingAllowed(false);

		
	
		jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
				new String[] { "Voornaam", "Tussen", "Achternaam",
						"Geboortedatum", "Woonplaats" , "adres","postcode","TelefoonNr","Startdatum","Uren" }));
		jScrollPane1.setViewportView(jTable1);
		jTable1.setPreferredScrollableViewportSize(new Dimension(850, 600));
		jTable1.setFillsViewportHeight(true);
		
		jTable1.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent e){
				if (e.getComponent().isEnabled() && e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2){
					Point p = e.getPoint();
					int row = jTable1.rowAtPoint(p); 
					int column = jTable1.columnAtPoint(p); 

					Object obj = jTable1.getModel().getValueAt(row, column);
					if(column != 8 && column != 10){
						String omschr = (String) obj;
						new WeergavePopup(omschr);
					}
				}
			}
		});
		
		jButton1.setText("toevoegen");

		jButton2.setText("Wijzigen");

		jButton3.setText("Verwijderen");

		jButton4.setText("Rooster");
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(
														jButton4,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jButton3,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jButton2,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jButton1,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE))
								.addContainerGap(563, Short.MAX_VALUE))
				.addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(
										layout.createSequentialGroup()
												.addGap(130, 200, Short.MAX_VALUE)
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGap(130, 200, Short.MAX_VALUE))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jButton1)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jButton2)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jButton3)
									.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jButton4)
									.addContainerGap(335, Short.MAX_VALUE))
				.addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(
										layout.createSequentialGroup()
												.addGap(0, 0, Short.MAX_VALUE)
												.addComponent(
														jScrollPane1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addGap(0, 0, Short.MAX_VALUE))));
	}

	@Override
	public void update(Observable o, Object arg) {
		this.inhoud = null;
		jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
				new String[] { "BSN","Voornaam", "Tussen", "Achternaam",
						"Geboortedatum", "Woonplaats" , "adres","postcode","TelefoonNr","Startdatum","Uren" }));
		PraktijkLijst lijst = (PraktijkLijst) o;
		int aantaltherapeuten = 0;
		for (int x = 0; x <  lijst.stuurLijst().size() ; x++ ){
			for(int y = 0 ; y <lijst.stuurLijst().get(x).getTherapeuten().size(); y++){
				aantaltherapeuten++;
				
			}
		}
		
		
		int aantal = 0;
		Object[][] inhoud = new Object[aantaltherapeuten][11];
		for ( int a =0; a < lijst.stuurLijst().size(); a++){
			
				for ( int b =0; b <lijst.stuurLijst().get(a).getTherapeuten().size(); b++){
		
		
				
				inhoud[aantal][0] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getBsn();
				inhoud[aantal][1] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getVoorNaam();
				inhoud[aantal][2] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getTussenVoegsel();
				inhoud[aantal][3] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getAchterNaam();
				inhoud[aantal][4] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getGebDatum();
				inhoud[aantal][5] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getWoonplaats();
				inhoud[aantal][6] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getAdres();
				inhoud[aantal][7] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getPostcode();
				inhoud[aantal][8] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getTelNr();
				inhoud[aantal][9] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getStartDatum();
				inhoud[aantal][10] = (Object) lijst.stuurLijst().get(a)
						.getTherapeuten().get(b).getUren();
				aantal++;
					
			}
			
			
			jTable1.setModel(new javax.swing.table.DefaultTableModel(inhoud,
					new String[] { "BSN","Voornaam", "Tussen", "Achternaam",
							"Geboortedatum", "Woonplaats" , "adres","postcode","TelefoonNr","Startdatum","Uren" }));

		}
		
	}


	public String getSelected() {
		if(jTable1.getSelectedRow() <0){
			return "fout";
		}
		else {
			return jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
		}

	}

}
